package aod.android.com.bernay.http;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.qiniu.util.Auth;

public class TempUpdataService extends Service {


    public static TempUpdataService mTempUpdataService;

    public static final String ACCESSKEY = "GoYfLUnOJhjzSj69KneLnBMLYoeQ2j8YYmwsW2Ex";
    public static final String SECRETKEY = "nwEFZCmtsI7v3KxPpDaInIabmPXQCFoqJMW3ontx";
    public static final String BUCKET    = "test";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mTempUpdataService = this;
    }


    public void getToken(final OnGetTokenListener onGetTokenListener){

        new Thread(){
            @Override
            public void run() {
                Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
                String upToken = auth.uploadToken(BUCKET);
                System.out.println(upToken);
                if(TextUtils.isEmpty(upToken)){
                    onGetTokenListener.onFild(new TokenFildException());
                }else {
                    onGetTokenListener.onSuccess(upToken);
                }


            }
        }.start();

    }


    public interface OnGetTokenListener{
        void onSuccess(String token);
        void onFild(Exception e);
    }


    public class TokenFildException extends Exception{
        public TokenFildException() {
            super("Token获取失败！");
        }
    }
}
